package com.definesys.smecrailtransit.module.LineReport.viewController;

import android.content.Context;
import android.graphics.Color;

import com.definesys.smecrailtransit.R;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.math.BigDecimal;
import java.util.ArrayList;

import static java.math.BigDecimal.ROUND_HALF_DOWN;


/**
 * Created by cjy on 2018/1/5.
 */

public class PieChartController {

    private PieChart chart;
    private Context context;
    private int totalCount;
    private int currentFaultCount;
    private int currentOfflineCount;
    private int currentNormalCount;
    private float faultPer;
    private float offlinePer;
    private float normalPer;

    public PieChartController(PieChart chart,Context context){
        this.chart = chart;
        this.context = context;


    }

    public void setData(int faultCount,int offlineCOunt, int normalCount){
        this.totalCount = faultCount + offlineCOunt + normalCount;
        this.currentFaultCount =faultCount;
        this.currentNormalCount = normalCount;
        this.currentOfflineCount = offlineCOunt;

        //计算三种状态电梯的百分比
        BigDecimal faultB = new BigDecimal(currentFaultCount).divide(new BigDecimal(totalCount),2,ROUND_HALF_DOWN);
        this.faultPer =  faultB.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();

        BigDecimal offlineB = new BigDecimal(currentOfflineCount).divide(new BigDecimal(totalCount),2,ROUND_HALF_DOWN);
        this.offlinePer =  offlineB.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();

        BigDecimal normalB = new BigDecimal(currentNormalCount).divide(new BigDecimal(totalCount),2,ROUND_HALF_DOWN);
        this.normalPer =  normalB.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();

        //处理因四舍五入造成的数据错误
        if((faultPer+offlinePer+normalPer) != 1){
            float gap = (faultPer+offlinePer+normalPer) -1;
            normalPer = normalPer - gap;
        }

    }

    public void initPieView(){
        chart.setUsePercentValues(true);//设置value是否用显示百分数,默认为false
//        chart.setDescription("");//设置描述
//        chart.setDescriptionTextSize(10);//设置描述字体大小
        //chart.setDescriptionColor(); //设置描述颜色
        //chart.setDescriptionTypeface();//设置描述字体

        chart.setExtraOffsets(5, 5, 5, 5);//设置饼状图距离上下左右的偏移量

        chart.setDragDecelerationFrictionCoef(0.99f);//设置阻尼系数,范围在[0,1]之间,越小饼状图转动越困难

        chart.setDrawCenterText(true);//是否绘制中间的文字
        chart.setCenterTextColor(Color.RED);//中间的文字颜色
        chart.setCenterTextSize(24);//中间的文字字体大小

        chart.setDrawHoleEnabled(true);//是否绘制饼状图中间的圆
        chart.setHoleColor(Color.WHITE);//饼状图中间的圆的绘制颜色
        chart.setHoleRadius(54f);//饼状图中间的圆的半径大小

        //chart.setTransparentCircleColor(Color.BLACK);//设置圆环的颜色
        chart.setTransparentCircleAlpha(110);//设置圆环的透明度[0,255]
        chart.setTransparentCircleRadius(60f);//设置圆环的半径值

        // enable rotation of the chart by touch
        chart.setRotationEnabled(true);//设置饼状图是否可以旋转(默认为true)
        chart.setRotationAngle(10);//设置饼状图旋转的角度

        chart.setHighlightPerTapEnabled(true);//设置旋转的时候点中的tab是否高亮(默认为true)

        Legend l = chart.getLegend();
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);//设置每个tab的显示位置
        l.setXEntrySpace(0f);
        l.setYEntrySpace(0f);//设置tab之间Y轴方向上的空白间距值
        l.setYOffset(0f);

        // entry label styling
        chart.setDrawEntryLabels(false);//设置是否绘制Label
        chart.setEntryLabelColor(Color.WHITE);//设置绘制Label的颜色
        //chart.setEntryLabelTypeface(mTfRegular);
        chart.setEntryLabelTextSize(10f);//设置绘制Label的字体大小

        //chart.setOnChartValueSelectedListener(this);//设值点击时候的回调
        chart.animateY(1000, Easing.EasingOption.EaseInQuad);//设置Y轴上的绘制动画

        ArrayList<PieEntry> pieEntries = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        if(currentNormalCount != 0){
            PieEntry pieEntry1 = new PieEntry(normalPer, "正常");
            pieEntries.add(pieEntry1);
            colors.add(context.getResources().getColor(R.color.line_report_green));
        }
        if(currentOfflineCount != 0) {
            PieEntry pieEntry2 = new PieEntry(offlinePer, "离线");
            pieEntries.add(pieEntry2);
            colors.add(context.getResources().getColor(R.color.line_report_gray));
        }
        if(currentNormalCount != 0) {
            PieEntry pieEntry3 = new PieEntry(faultPer, "异常");
            pieEntries.add(pieEntry3);
            colors.add(context.getResources().getColor(R.color.line_report_red));
        }

        PieDataSet pieDataSet = new PieDataSet(pieEntries, "");

        pieDataSet.setColors(colors);
        pieDataSet.setDrawValues(false);

        pieDataSet.setSliceSpace(3f);//设置选中的Tab离两边的距离
        pieDataSet.setSelectionShift(5f);//设置选中的tab的多出来的
        PieData pieData = new PieData();
        pieData.setDataSet(pieDataSet);

        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextSize(12f);
        pieData.setValueTextColor(Color.WHITE);

//        chart.setDescription("正常:"+currentNormalCount+"台| 离线:"+currentOfflineCount+"台| 异常:"+currentFaultCount+"台");


        chart.setData(pieData);
        // undo all highlights
        chart.highlightValues(null);
        chart.invalidate();
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getCurrentFaultCount() {
        return currentFaultCount;
    }

    public void setCurrentFaultCount(int currentFaultCount) {
        this.currentFaultCount = currentFaultCount;
    }

    public int getCurrentOfflineCount() {
        return currentOfflineCount;
    }

    public void setCurrentOfflineCount(int currentOfflineCount) {
        this.currentOfflineCount = currentOfflineCount;
    }

    public int getCurrentNormalCount() {
        return currentNormalCount;
    }

    public void setCurrentNormalCount(int currentNormalCount) {
        this.currentNormalCount = currentNormalCount;
    }
}
